// main layer
var g_towerManager=null;
var TowerManager = cc.Node.extend({
    _menus : [],
    _towers : [],
    _jsonObjs : [],
    _localFlag : null,
    _point : cc.p(0,0),
    ctor:function ( configfilename ) {
        this._super();
        // reference this
        g_towerManager=this;
        // add local flag
        this.addLocalFlag( "res/ok.jpg" );
        // init json object
        this.initJsonObjs(configfilename);
        // add map
        this.initTowerMenu();
        return true;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._menus.splice(0,this._menus.length);
        this._towers.splice(0,this._towers.length);
        this._jsonObjs.splice(0,this._jsonObjs.length);
        this._super();
    },
    addLocalFlag : function( filename ){
        this._localFlag = new cc.Sprite( filename );
        this._localFlag.setPosition( cc.p( cc.winSize.width*0.5,cc.winSize.height*0.5 ) );
        this._localFlag.setVisible(false);
        this.addChild(this._localFlag);
    },
    initJsonObjs : function(configfilename){
        var configJson=CreateJsonWithFile(configfilename);
        if( configJson ){
            for ( var i=0;i<configJson.towerFiles.length;i++){
                var filename=configJson.towerFiles[i];
                var jsonObj=CreateJsonWithFile(filename);
                this._jsonObjs.push(jsonObj);
            }
            cc.log("TowerManager,init down,length: "+this._jsonObjs.length);
        }else{
            cc.log("TowerManager,json invalid,file name: "+configfilename);
        }
    },
    initTowerMenu : function(){
        if(g_gameLevel>=0&&g_gameLevel<this._jsonObjs.length){
            var jsonObject=this._jsonObjs[g_gameLevel];
            if(jsonObject) {
                var towers=[];
                // getting player's gold
                var gold = getUILayerInstance().getMoney();
                for ( var i=0;i<jsonObject.towers.length;i++){
                    var data=jsonObject.towers[i];
                    if( data.tRes ){
                        var resName = "res/"+data.tRes;
                        var tower = new cc.MenuItemImage(resName,resName,this.onChooseTower,this);
                        tower.setPosition( cc.p(-100,-100) );
                        if ( gold<data.cst){
                            this.setTowerEnable(tower,false);
                        }else{
                            this.setTowerEnable(tower,true);
                        }
                        this._menus.push(tower);
                        towers.push(tower);
                        tower.setUserData( data );
                    }
                }
                var menu = cc.Menu.create( towers );
                this.addChild( menu );
                // setting position
                menu.setPosition( cc.p(0,0) );
            }else{
                cc.log("json object is invalid");
            }
        }else{
            cc.log("there is no this scene,g_gameLevel: "+g_gameLevel);
            g_gameLevel = 0;
            cc.log("resetting g_gameLevel,g_gameLevel: "+g_gameLevel);
        }
    },
    // setting tower enable or not
    setTowerEnable : function(tower,enable){
        if(enable){
            tower.setColor(cc.color.WHITE);
        }else{
            tower.setColor(cc.color(135,123,103));
        }
    },
    // hide towers
    hideTower : function(){
        for ( var i=0;i<this._menus.length;i++){
            var tower=this._menus[i];
            tower.setPosition( cc.p(-100,-100) );
        }
    },
    // layout towers
    layoutTower : function(basePos){
        for ( var i=0;i<this._menus.length;i++){
            var tower=this._menus[i];
            tower.setPosition( cc.p(basePos.x-(this._menus.length-2)*0.5*80+i*80,basePos.y+110) );
        }
    },
    // refresh enable towers
    refreshTower : function(){
        if( this._menus[0].getPositionX() < 0 ){
            var iu = getUILayerInstance();
            var gold=iu.getMoney();
            for ( var i=0;i<this._menus.length;i++){
                var tower=this._menus[i];
                if(tower){
                    var cost=tower.getUserData().cst;
                    if ( gold<cost){
                        this.setTowerEnable(tower,false);
                    }else{
                        this.setTowerEnable(tower,true);
                    }
                }
            }
        }
    },
    // choose tower to locating
    onChooseTower : function(sender){
        var iu = getUILayerInstance();
        var data = sender.getUserData();
        var cost = data.cst;
        if ( iu.getMoney()>=cost){
            iu.addMoney( -cost );
            this.addTower( data );
            this.hideTower();
        }else{
            cc.log("no money");
        }
    },
    //////////////////////////////////////////////////////////
    showTowers : function( touch ){
        if( !this._localFlag.isVisible() ){

            this.refreshTower();
            var local = touch.getLocation();
            var mainLayer=getMainLayerInstance();

            var jsonObj = mainLayer._jsonObjs[g_gameLevel];
            var point = PixelToPoint(jsonObj,local);

            this._localFlag.setPosition(cc.p((point.x+0.5)*jsonObj.width,
                    (point.y+0.5)*jsonObj.height));
            this._localFlag.setVisible(true);
            // checking is good to locating tower
            var ret = mainLayer._jsonMap.isEmptyPoint( point );
            if( ret ){
                this._point = point;
                var pixel = pointToPixel(jsonObj,point);
                this.layoutTower(pixel);
            }else{
                cc.log("this position is not empty");
            }
        }else{
            this._localFlag.setVisible(false);
            this.hideTower( );
        }
    },
    addTower : function( data ){
        cc.log("add tower");
        var mainLayer=getMainLayerInstance();
        var localObj = this._localFlag;
        var resName = "res/"+data.tRes;
        var tower = TowerA.create( resName ,data );
        tower.setPosition( localObj.getPosition() );
        mainLayer.addChild( tower,localObj.getZOrder() );
        localObj.setVisible(false);
        this.hideTower();
        // save point for splice itself then register it
        tower.setPoint(this._point);
        this.register(this._point,tower);
    },
    register : function(x,y,registerobj){
        getMainLayerInstance()._jsonMap.register(x,y,registerobj);
        this.pushTower(registerobj);
    },
    register : function(point,registerobj){
        getMainLayerInstance()._jsonMap.register(point,registerobj);
        this.pushTower(registerobj);
    },
    unregister : function(x,y,index){
        getMainLayerInstance()._jsonMap.unregister(x,y);
        this.spliceTower(index);
    },
    unregister : function(point,index){
        getMainLayerInstance()._jsonMap.unregister(point);
        this.spliceTower(index);
    },
    pushTower : function(tower){
        // you must setting index before push into tower buffer
        tower.setIndex(this._towers.length);
        // push into tower buffer
        this._towers.push(tower);
        cc.log("push down,current tower buffer: "+this._towers.length);
    },
    spliceTower : function(index){
        if(index>=0&&index<this._towers.length){
            cc.log("splice down,current index: "+index);
            this._towers.splice(index,1);
        }else{
            cc.log("index overflow,current index: "+index);
        }
    },
    getTowers : function(){
        return this._towers;
    },
    onTower : function(sender){
        cc.log("on tower,update tower or recover it");
    }
});


